/*
 * Viewer.h
 * Authors: 107 <107@mail.ru>
 * License: GPL >=2.0
 *
 *
 */

#ifndef VIEWER_H_
#define VIEWER_H_

#include <FApp.h>
#include <FBase.h>
#include <FUi.h>
#include <FIo.h>
#include <FGraphics.h>
#include <FContent.h>

// XXX: platform defined. see strlen on constructor
#define MAX_BUFF_LEN 600 // max chars in screen (buffer) for exmaple 1/2 of max Length of EditArea

/**
 * EnricehdText based class with options and TextElement encluded
**/
class EText :
	public Osp::Graphics::EnrichedText {
	public:
		Osp::Graphics::TextElement	*pTextElement;
		EText ( Osp::Graphics::Dimension d );
		void SetText ( Osp::Base::String *str );
		~EText ( void );
};

/*
 * Can render to canvas come buffer
 */
class Viewer {
private:
				Osp::Graphics::Dimension	d;
	virtual		Osp::Graphics::Canvas		*GetCanvas ( void ) {}; // Define this in child!
protected:
	EText						*pEText;
	void 	DrawViewer ( void );
public:
	Viewer  ( void );
	~Viewer ( void );
	Osp::Graphics::Rectangle GetViewerBounds ( void );
	Osp::Graphics::Dimension GetViewerDimension ( void );
	virtual void SetSize ( Osp::Graphics::Dimension dd );
	void SetText ( Osp::Base::String *str );
};

/*
 * Progress bar base class
 * please, define virtual functions on your child class
 */
class VProgress {
private:
	virtual		Osp::Graphics::Canvas		*GetCanvas ( void ) {}; // Define this in child!
				Osp::Graphics::Dimension	d;
protected:
	int							prev_p;           // prev value of percent
	int							cur_p;           // current value of percent
	virtual int GetMaxIndex ( bool=false ) { return 1; };
	virtual int GetCurrentIndex ( bool=false ) { return 1; };
	virtual void GotoIndex ( int position ) {};
	void DrawProgress ( bool force );
public:
	VProgress  ( void );
	~VProgress ( void );
	Osp::Graphics::Rectangle GetProgressBounds ();
	virtual void SetSize ( Osp::Graphics::Dimension dd ) { d=dd; };
	void OnProgressTouch ( int y ); 					// You must call it when user touch progress bar to scroll

};

/*
 * Can view Full text by show it on Viewer and scroll with VProgress
 */
class ScrolledViewer :
	public VProgress, public Viewer {
private:
		Osp::Graphics::Dimension	d;
		Osp::Base::String			sBuff;			// buffer
		Osp::Base::String			sFullText;			// TODO: I don't like store full text in String.
														// Maybe use bytebuffer or list<String> / <list>mchar
		EText						*pEFullText;
		Osp::Graphics::Canvas		*pCanvas;			// virtual canvas
		// for scroll , see VProgress
		int							_cache_current_index;			// current line num
		int							_cache_max_index;				// total line count
		// virtual
		int					GetCurrentIndex ( bool );   // current line num
		int					GetMaxIndex ( bool ); 		// totali line count
		Osp::Graphics::Canvas		*GetCanvas ( void ); //
		// other
		Osp::Base::String	*GetTextP ( void ); 		// return pointer to full text (for modify f.ex.)
		int 				GetCurrentPosition ( void ); //
		int	position; // current position Index of Text

protected:
		Osp::Base::String			sFileName;			// name of current file
		virtual void				msg ( const Osp::Base::String title, const Osp::Base::String text  ) {}; // If I want to say smth to user. I use that function
		virtual	Osp::Graphics::Canvas		*GetRealCanvasN ( void ) { AppLog( "Please define GetRealCanvasN in child!" ); }; // Need define real canvas on child
		void 				SetSize ( Osp::Graphics::Dimension dd );
public:
		ScrolledViewer  ( void );
		~ScrolledViewer ( void );

		Osp::Graphics::Rectangle GetCanvasBounds ( void );
		void 				RenderCurrentPosition ( bool force=false ); // render text on current position
		void 				LoadFromFile ( const Osp::Base::String fname );
		void 				SaveToFile ( const Osp::Base::String & fname );
		void 				InsertText ( Osp::Base::String str );
		Osp::Base::String	GetCurrentBuff ( void );    // return current rendered text (f.ex. for load to editor)
		void				Clear ( void );
		void				GotoIndex ( const int num );
		void 				Scroll ( const int lines ); // + or - lines to scroll
};


#endif /* VIEWER_H_ */
